package com.doit.demo.day02.transformations;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @DATE 2022/2/27/11:23
 * @Author MDK
 * @Version 2021.2.2
 *
 *      使用lambda表达式完成map功能,返回的DataStream的数据类型还有泛型,程序再编译的话,泛型即会丢失
 *      必须调用returns方法指定返回的数据类型
 *
 *    比如：DataStream<Boy> 不用加returns
 *    比如：DataStream<MyList<Boy>> 要加returns
 **/
public class MapDemo3 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> lines = env.socketTextStream("linux01", 8888);

        SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndCount = lines.map(line -> {
            String[] fields = line.split(",");
            String word = fields[0];
            int count = Integer.parseInt(fields[1]);
            return Tuple2.of(word, count);
        }).returns(TypeInformation.of(new TypeHint<Tuple2<String, Integer>>() {}));
        //.returns(Types.TUPLE(Types.STRING, Types.INT));
        //另一种写法,如果使用lambda表达式,返回的DataStream有类型嵌套,必须returns指定返回的数据类型


        wordAndCount.print();

        env.execute();
    }
}
